package com.lu.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lu.eneity.Role;
import com.lu.eneity.User;
import com.lu.repository.BlogRepository;
import com.lu.repository.ItemRepository;
import com.lu.repository.RoleRepository;
import com.lu.repository.UserRepository;
@Transactional
@Service
public class InitDbService {
	@Autowired
	private BlogRepository blogRepository;
	@Autowired
	private ItemRepository itemRepository;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private RoleRepository roleRepository;

	@PostConstruct
	private void init() {
		Role roleUser = new Role();
		roleUser.setName("ROLE_USER");
		roleRepository.save(roleUser);
		Role roleAdmin = new Role();
		roleUser.setName("ROLE_Admin");
		roleRepository.save(roleAdmin);

		User userAdminUser = new User();
		userAdminUser.setName("admin");
		List<Role> roles = new ArrayList<Role>();
		roles.add(roleAdmin);
		roles.add(roleUser);
		userAdminUser.setRoles(roles);
		userRepository.save(userAdminUser);

	}

}
